package view.listeners;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JPanel;

import model.Constants;
import model.Subnet;
import view.components.SubnetComboBox;
import view.dialogs.MultiIpAddDialog;
import view.views.dialogContentViews.MultiAddIpContentPanel;
import view.views.dialogContentViews.MultiListPanel;
import controller.Controller;

public class SizeBoxListener implements ItemListener {

	private final SubnetComboBox subnetCombo;
	private final MultiIpAddDialog multiIpAddDialog;
	private final JPanel multiPanel;
	private final MultiAddIpContentPanel content;
	private MultiListPanel multiIpList;

	public SizeBoxListener(MultiIpAddDialog multiIpAddDialog,
			SubnetComboBox subnetCombo, JPanel multiPanel,
			MultiListPanel multiIpList, MultiAddIpContentPanel content) {
		this.multiIpAddDialog = multiIpAddDialog;
		this.subnetCombo = subnetCombo;
		this.multiPanel = multiPanel;
		this.content = content;
		this.multiIpList = multiIpList;
	}


	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == 2) {
			Subnet sub = null;
			if (!subnetCombo.getBox().getSelectedItem()
					.equals(Constants.EMPTY)) {
				sub = subnetCombo.getMap().get(
						subnetCombo.getBox().getSelectedItem());

				if (!(multiPanel.getComponents().length == 0)) {
					multiPanel.remove(0);
				}
				multiIpList = new MultiListPanel(sub);
				Controller.getInstance().setMultiList(multiIpList);
				multiPanel.add(multiIpList);
					content.add(multiPanel);
				
			} else {
				multiPanel.remove(0);
			}

			multiIpAddDialog.pack();

	}

}
}